//
//  ViewController.m
//  09-homework-province2
//
//  Created by 王道林 on 16/7/26.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "TownViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *arrPro;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createDate];
    [self creatSubView];
}

- (void)createDate
{
    NSString *path = @"/Users/wangdao/DLS160710/UI/HomeWork/09-homework-province-city/09-homework-province-city/area.txt";
    NSString *stringArea = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrAll = [stringArea componentsSeparatedByString:@"\n"];
    _arrPro = [NSMutableArray array];
    for (NSString *string in arrAll) {
        if (![string hasPrefix:@" "]) {
            NSMutableDictionary *dicPro = [NSMutableDictionary dictionary];
            [dicPro setValue:string forKey:@"Province"];
            //创建市的数组
            NSMutableArray *arrCity = [NSMutableArray array];
            [dicPro setValue:arrCity forKey:@"city"];
            [_arrPro addObject:dicPro];
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionary];
            [dicCity setObject:string forKey:@"cityName"];
            //创建区的数组
            NSMutableArray *arrayTown = [NSMutableArray array];
            [dicCity setObject:arrayTown forKey:@"town"];
            [[_arrPro lastObject][@"city"] addObject:dicCity];
        } else {
            [[[_arrPro lastObject][@"city"] lastObject][@"town"] addObject:string];
        }
    }
    NSLog(@"%@", _arrPro);
    
}

- (void)creatSubView
{
    _tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return _arrPro.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = _arrPro[section];
    NSArray *arr = dic[@"city"];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wang"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"wang"];
    }
   NSString *province = _arrPro[indexPath.section][@"city"][indexPath.row][@"cityName"];
    NSArray *arr = [province componentsSeparatedByString:@" "];
    cell.textLabel.text = [arr objectAtIndex:2];
    cell.detailTextLabel.text = [arr lastObject];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _arrPro[section][@"Province"];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TownViewController *townVc = [[TownViewController alloc] init];
   townVc.marrTown = _arrPro[indexPath.section][@"city"][indexPath.row][@"town"];
    
    [self.navigationController pushViewController:townVc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
